package com.sgcc.fj.pmos.mobileapp.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvcNetCallback;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.mobileapp.bean.PowLevelAndTypeBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 新增户号-----增量配电网弹窗
 */
public class AccountIncrementPopup extends CenterPopupView {
    private LinearLayout linner_type;
    private TextView tv_cancel, tv_confirm;
    private TextView tv_voltagegrade, tv_electricitytype, tv_pricetype, tv_mvoltcode, tv_electricitytype_2;
    private EditText eidt_consno, edit_marketingusername, edit_address, edit_membersnamepointid, edit_pointname, edit_mpcap, edit_lastyearpq;

    Map<String, Object> params = new HashMap<>();
    List<PowLevelAndTypeBean.DataDTO> powLevel = new ArrayList<>();
    List<PowLevelAndTypeBean.DataDTO> powType = new ArrayList<>();
    String[] powLevel_show;
    String[] powType_show;
    String[] pricetypes;
    boolean isClick;

    public AccountIncrementPopup(@NonNull Context context, boolean isClick, OnClickListener onClickListener) {
        super(context);
        this.isClick = isClick;
        this.onClickListener = onClickListener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.account_increment_pop;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        String[] strings = new String[3];
        strings[0] = "国家电网";
        strings[1] = "地方电网";
        strings[2] = "增量配电网";

        pricetypes = new String[2];
        pricetypes[0] = "单一制";
        pricetypes[1] = "两部制";
        getPowType();
        getPowLevel();
        initTextAndEdit();
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                params.put("powerGridType", "3");
                if (!TextUtils.isEmpty(eidt_consno.getText().toString())) {
                    params.put("consNo", eidt_consno.getText().toString());
                } else {
                    ToastUtils.showShort("请填写用户编号");
                    return;
                }
                if (!TextUtils.isEmpty(edit_marketingusername.getText().toString())) {
                    params.put("marketingUserName", edit_marketingusername.getText().toString());
                } else {
                    ToastUtils.showShort("请填写用户名称");
                    return;
                }
                if (!TextUtils.isEmpty(edit_address.getText().toString())) {
                    params.put("address", edit_address.getText().toString());
                } else {
                    ToastUtils.showShort("请填写地址");
                    return;
                }
                if (!TextUtils.isEmpty(edit_membersnamepointid.getText().toString())) {
                    params.put("membersNamepointId", edit_membersnamepointid.getText().toString());
                } else {
                    ToastUtils.showShort("请填写计量点");
                    return;
                }
                if (!TextUtils.isEmpty(edit_pointname.getText().toString())) {
                    params.put("pointName", edit_pointname.getText().toString());
                } else {
                    ToastUtils.showShort("请填写计量点名称");
                    return;
                }
                if (!TextUtils.isEmpty(edit_mpcap.getText().toString())) {
                    params.put("mpCap", edit_mpcap.getText().toString());
                } else {
                    ToastUtils.showShort("请填写计量点容量");
                    return;
                }
                if (!TextUtils.isEmpty(edit_lastyearpq.getText().toString())) {
                    params.put("lastyearPq", edit_lastyearpq.getText().toString());
                } else {
                    ToastUtils.showShort("请填写月均用电量");
                    return;
                }

                tv_voltagegrade = findViewById(R.id.tv_voltagegrade);
                tv_electricitytype = findViewById(R.id.tv_electricitytype);
                tv_pricetype = findViewById(R.id.tv_pricetype);
                tv_mvoltcode = findViewById(R.id.tv_mvoltcode);
                tv_electricitytype_2 = findViewById(R.id.tv_electricitytype_2);
                if (TextUtils.isEmpty("" + params.get("voltageGrade"))) {
                    ToastUtils.showShort("请选择电压等级");
                    return;
                }
                if (TextUtils.isEmpty("" + params.get("electricityType"))) {
                    ToastUtils.showShort("请选择用电类别");
                    return;
                }
                if (TextUtils.isEmpty("" + params.get("price"))) {
                    ToastUtils.showShort("请选择价格类别");
                    return;
                }
                if (TextUtils.isEmpty("" + params.get("mvoltCode"))) {
                    ToastUtils.showShort("请选择计量点电压等级");
                    return;
                }
                if (onClickListener != null) {
                    onClickListener.onPositiveClick(AccountIncrementPopup.this, params);
                }
            }
        });
        linner_type = findViewById(R.id.linner_type);
        if (isClick)
            linner_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new XPopup.Builder(getContext())
                            .hasShadowBg(false)
                            .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                            .asAttachList(strings,
                                    null,
                                    new OnSelectListener() {
                                        @Override
                                        public void onSelect(int position, String text) {
                                            if (onClickListener != null) {
                                                onClickListener.onTypeClick(AccountIncrementPopup.this, position);
                                            }
                                        }
                                    }, 0, 0, Gravity.CENTER).show();
                }
            });
    }

    private void initTextAndEdit() {
        tv_voltagegrade = findViewById(R.id.tv_voltagegrade);
        tv_electricitytype = findViewById(R.id.tv_electricitytype);
        tv_pricetype = findViewById(R.id.tv_pricetype);
        tv_mvoltcode = findViewById(R.id.tv_mvoltcode);
        tv_electricitytype_2 = findViewById(R.id.tv_electricitytype_2);

        eidt_consno = findViewById(R.id.eidt_consno);
        edit_marketingusername = findViewById(R.id.edit_marketingusername);
        edit_address = findViewById(R.id.edit_address);
        edit_membersnamepointid = findViewById(R.id.edit_membersnamepointid);
        edit_pointname = findViewById(R.id.edit_pointname);
        edit_mpcap = findViewById(R.id.edit_mpcap);
        edit_lastyearpq = findViewById(R.id.edit_lastyearpq);

        tv_voltagegrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false).popupWidth(600)
                        .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(powLevel_show,
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        tv_voltagegrade.setText(text);
                                        params.put("voltageGrade", powLevel.get(position).getCode());
                                    }
                                }, 0, 0, Gravity.CENTER).show();
            }
        });

        tv_electricitytype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false)
                        .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(powType_show,
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        tv_electricitytype.setText(text);
                                        tv_electricitytype_2.setText(text);
                                        params.put("electricityType", powType.get(position).getCode());
                                    }
                                }, 0, 0, Gravity.CENTER).show();
            }
        });
        tv_pricetype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false)
                        .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(pricetypes,
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        tv_pricetype.setText(text);
                                        if (position == 0) {
                                            params.put("price", "01");
                                        } else if (position == 1) {
                                            params.put("price", "02");
                                        }

                                    }
                                }, 0, 0, Gravity.CENTER).show();
            }
        });


        tv_mvoltcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false)
                        .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(powLevel_show,
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        tv_mvoltcode.setText(text);
                                        params.put("mvoltCode", powLevel.get(position).getCode());
                                    }
                                }, 0, 0, Gravity.CENTER).show();
            }
        });


        tv_electricitytype_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false)
                        .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(powType_show,
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        tv_electricitytype.setText(text);
                                        tv_electricitytype_2.setText(text);
                                        params.put("electricityType", powType.get(position).getCode());
                                    }
                                }, 0, 0, Gravity.CENTER).show();
            }
        });

    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .8f);
    }

    @Override
    protected int getMaxWidth() {
        return (int) (XPopupUtils.getScreenWidth(getContext()) * .9f);
    }

    @Override
    protected boolean onBackPressed() {
        return true;
    }


    private void getPowLevel() {
        Map<String, Object> params = new HashMap<>();
        params.put("groupCode", "voltageGrade");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_POWERSUERREG, new MvcNetCallback<PowLevelAndTypeBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
            }

            @Override
            public void onResponse(PowLevelAndTypeBean response, int id) {
                super.onResponse(response, id);
                if (response != null && response.getStatus() == 0) {
                    powLevel = response.getData();
                    powLevel_show = new String[powLevel.size()];
                    for (int i = 0; i < powLevel.size(); i++) {
                        powLevel_show[i] = powLevel.get(i).getName();
                    }
                }
            }
        });
    }

    private void getPowType() {
        Map<String, Object> params = new HashMap<>();
        params.put("groupCode", "electricityType");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_POWERSUERREG, new MvcNetCallback<PowLevelAndTypeBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
            }

            @Override
            public void onResponse(PowLevelAndTypeBean response, int id) {
                super.onResponse(response, id);
                if (response != null && response.getStatus() == 0) {
                    powType = response.getData();
                    powType_show = new String[powType.size()];
                    for (int i = 0; i < powType.size(); i++) {
                        powType_show[i] = powType.get(i).getName();
                    }
                }
            }
        });
    }


    /**
     * 设置确定取消按钮的回调
     */
    public OnClickListener onClickListener;


    public interface OnClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(AccountIncrementPopup popup, Map<String, Object> params);

        /**
         * 点击确定按钮事件
         */
        public void onTypeClick(AccountIncrementPopup popup, int position);

    }
}
